// pages/LNE_address/LNE_address.js


const http = require('../../ajax.js')
const api = require('../../api.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据   
   */
  data: {
    titles: ["送货上门", "到店自取"],// 定义标题的数组
    selectedTitle: "0",   // 定义选中标题的初始值0
    src: "",
    currentCity: '',
    jingweidu: '',
    dizhi: true,
    ditu: "",//地图城市
    city: "",//当前城市
    sematic_description: "",//当前城市
    animation: '',
    juli: '',
    shouAddress:'',
    person:false
   
  },
  //跳返回首页地址
  backhome(){
    wx.navigateBack({
      url: '/pages/home/home'
    })
  },
  bindtap: function (e) {
    this.setData({
      selectedTitle: e.currentTarget.id
    });
  },
  bindChange: function (e) {
    this.setData({
      selectedTitle: e.detail.current
    })
  },
  replace() {
    var item = wx.getStorageSync("a")[0]
    console.log(item)
    app.globalData.shouhuo = item;
    console.log("quabju", app.globalData.shouhuo)
    wx.navigateTo({
      url: '/pages/mine/replace/replace?' 
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadInfo();
    var address = wx.getStorageSync("a")
    if(this.data.shouAddress==undefined){

    }else{
      this.setData({
        shouAddress: address[0]
      })
      console.log(this.data.shouAddress)
      if (this.data.shouAddress.sex === 1) {
        this.setData({
          person: true
        })
      } else if (this.data.shouAddress.sex === 2) {
        this.setData({
          person: false
        })
      }
    }
    

    if (options.id === undefined) {
      ditu: this.data.city
    } else {
      this.setData({
        ditu: app.globalData.dizhitwo[options.id].chengshi
      })

    }
   
   
    if (this.data.shouAddress!="") {
      this.setData({
        dizhi: false
      })
    }

   

  },
  loadInfo: function () {
    var page = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log("当前位置", res)

        var longitude = res.longitude
        var latitude = res.latitude
        page.loadCity(longitude, latitude)
      }
    })
  },
  loadCity: function (longitude, latitude) {
    var page = this
    wx.request({
      url: 'http://api.map.baidu.com/geocoder/v2/?ak=HbUVYMUg6PwbOnXkztdgSQlQ&location=' + latitude + ',' + longitude + '&output=json&pois=1',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // success        
        var city = res.data.result.formatted_address;
        var sematic_description = res.data.result.business;
        var jingweidu = res.data.result.location
        page.setData({
          city: city,
          sematic_description: sematic_description,
          jingweidu: jingweidu
        });
        console.log(page.data.jingweidu);
        http(api.juli, "params",
          {
            lng1: 91.153653,
            lat1: 29.652041,
            lng2: page.data.jingweidu.lng,
            lat2: page.data.jingweidu.lat,
          },
          "POST"
        ).then(res => {
          console.log("juli", res)
          if (res.statusCode === 200) {
            page.setData({
              juli: res.data.result
            })
          }
        })
      }
    })

  },
  onChangeAddress: function (e) {
    wx.navigateTo({
      url: "/pages/LNE_position/LNE_position"
    });
  },
  address(){
    wx.navigateTo({
      url: "/pages/mine/increase/increase"
    });
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.animation = wx.createAnimation({
      // 动画持续时间 单位ms 默认 400
      duration: 1000,
      timingFunctionL: 'linear',
      delay: 100,
      transformOrigin: 'center',
      success: function (res) {
        console.log(res)
      }
    })
  },
  rotate: function () {
    // 顺时针旋转10度
    this.animation.rotate(180).step();
    this.setData({
      // 输出动画
      animation: this.animation.export()
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {


  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})